package com.ruirenyun.config.wx;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信支付配置类
 *
 * @author Deng Ye
 */
@Configuration
@ConditionalOnProperty(
        prefix = "wx",
        name = {"appId"}
)
@EnableConfigurationProperties(com.ruirenyun.config.wx.WxProperties.class)
public class WxConfiguration {
    @Autowired
    private WxProperties properties;

    @Bean
    public WxMaDefaultConfigImpl initStorage() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppId());
        config.setSecret(properties.getAppSecret());
        return config;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxMaService wxService() {
        WxMaServiceImpl wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(initStorage());
        return wxMaService;
    }
}
